package com.eas.biz.service.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eas.biz.dao.user.dataobject.CommitteeDO;
import com.eas.biz.dao.user.interfaces.CommitteeDao;
import com.eas.biz.framework.BaseQuery;
import com.eas.biz.service.user.domain.CommitteeInfo;
import com.eas.biz.service.user.domain.SchoolInfo;
import com.eas.biz.service.user.interfaces.CommitteeService;
import com.eas.biz.service.user.interfaces.SchoolService;

@Service("committeeService")
public class CommitteeServiceImpl implements CommitteeService {

    @Autowired
    private CommitteeDao committeeDao;

    @Resource(name = "schoolCacheService")
    private SchoolService schoolService;

    public CommitteeDO selectById(long id) {
        return committeeDao.selectById(id);
    }

    public long insert(CommitteeDO committee) {
        return committeeDao.insert(committee);
    }

    public int deleteById(long id) {
        return committeeDao.deleteById(id);
    }

    public int update(CommitteeDO committee) {
        return committeeDao.update(committee);
    }

    public List<CommitteeDO> selectByQuery(BaseQuery query) {
        return committeeDao.selectByQuery(query);
    }

    public int countByQuery(BaseQuery query) {
        return committeeDao.countByQuery(query);
    }

    public CommitteeInfo getCommitteeInfo(long id) {
        CommitteeDO committee = committeeDao.selectById(id);
        CommitteeInfo committeeInfo = buildCommitteeInfo(committee);
        return committeeInfo;
    }

    private CommitteeInfo buildCommitteeInfo(CommitteeDO committee) {
        if (committee != null) {
            CommitteeInfo committeeInfo = new CommitteeInfo();
            committeeInfo.setId(committee.getId());
            committeeInfo.setAreaId(committee.getAreaId());
            committeeInfo.setAreaName(committee.getAreaName());
            committeeInfo.setName(committee.getName());
            committeeInfo.setTimeCreate(committee.getTimeCreate());
            committeeInfo.setTimeModified(committee.getTimeModified());

            addSchoolInfo(committeeInfo);
            return committeeInfo;
        } else {
            return null;
        }
    }

    private void addSchoolInfo(CommitteeInfo committeeInfo) {
        List<SchoolInfo> schoolList = schoolService.getSchoolInfoList(committeeInfo.getAreaId());
        if (schoolList != null) {
            committeeInfo.getSchoolList().addAll(schoolList);
        }
    }

}
